package com.ncp.model.biz.comment;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;

public class CommentScoCl {
	
	private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	/**
	 * 得到物品的平均得分
	 * @param id物品ID
	 * @return
	 */
	public double getAvgSco(int id){
		double sco=0;
		int count=0;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement("select Score from  PRODUCTCOMMENT where ProductID="+id+"");
			rs=ps.executeQuery();
			while(rs.next()){
				sco+=rs.getInt(1);
			}
			ps=conn.prepareStatement("select count(*) from PRODUCTCOMMENT where ProductID="+id+"");
			rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getInt(1);
			}
			if(count==0){
				sco=0.0;
			}else {
				sco=sco/count;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try{
				if(rs!=null)
					rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			new DB().closeSQL(conn, ps);
		}
		return sco;
	}
	
	/**
	 * 格式化传入的分数
	 * @param sco分数
	 * @return 小数点后一位
	 */
	public float formatFloat(double sco){
		float a=0;
		String s=Double.toString(sco).substring(0,3);
		a=Float.parseFloat(s);
		return a;
	}
}
